package com.Modular.pdf_Util.examplesPDF.CheLiangDiaoDuRiZhiDengjiBiao;

import cn.hutool.core.date.DateUtil;

import com.Modular.MinioPlugin.MinIoOperations;
import com.Modular.pdf_Util.PDF_utils;
import com._Config.Model.errorLogModel;
import com.jfinal.core.Controller;
import com.jfinal.kit.Kv;
import com.jfinal.log.Log4jLog;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 车辆调度日志登记
 */
public class CheLiangDiaoDuRiZhiDengjiBiaoService extends Controller {
    static Log4jLog logger= Log4jLog.getLog(CheLiangDiaoDuRiZhiDengjiBiaoService.class);

    public static final CheLiangDiaoDuRiZhiDengjiBiaoService service = new CheLiangDiaoDuRiZhiDengjiBiaoService();

    public String buildPdf(String danWeiDaiMa) {

        try {
            String endTime = endDay(0);   //当月结束时间
            String yueFen= DateUtil.format(DateUtil.parseDate(endTime), "yyyy-MM");
            String startTime = yueFen + "-01 00:00:00"; //当月开始时间

            List<errorLogModel> dataList
                    = errorLogModel.dao.find("SELECT * FROM T_AnQuanShengChanRiChangGuanLi_CheLiangDiaoDuRiZhiGuanLi " +
                    " WHERE sys_xitongzhuangtai=0 AND SYS_DanWeiDaiMa=? and DiaoDuRiQi >=? AND DiaoDuRiQi <=? "
            ,danWeiDaiMa,startTime,endTime);

            List<Map<String , Object>> mapList=new ArrayList<>();
            String qiYeMingCheng = null;
            for (int i = 0; i < dataList.size(); i++) {
                mapList.add(diaoDuRiZhiToMap(dataList.get(i),i+1));
                qiYeMingCheng= (String) mapList.get(0).get("QiYeMingCheng");
            }
            String time=DateFormatUtils.format(new Date(),"yyyy-MM-dd");
            Map<String,String> titleMap=new HashMap<>();
            titleMap.put("QiYeMingCheng",qiYeMingCheng);
            titleMap.put("Time",time);

            String dir=System.getProperty("user.dir");
            String fileName=String.format("%s%s.pdf","车辆调度日志登记表", DateUtil.format(new Date(),"yyyyMMddHHmmss"));
            String outPath = String.format("%s%s%s",dir,"/webapp/WEB-INF/template/temp/",fileName);

            String pdfName = String.format("%s.pdf","车辆调度日志登记表-"+time);
             //生成本地pdf 一表单+一表格
            PDF_utils.buildPdf("车辆调度日志登记表-模板.docx",outPath,titleMap,mapList);
            logger.debug("fileName:%s,output:%s",fileName,outPath);
            MinIoOperations.fileUpload("asiatrip", pdfName,outPath);
            String geturl = MinIoOperations.fileGeturlGet("asiatrip", pdfName);
            return geturl;
        }catch(Exception e){
            throw new RuntimeException(e);
        }
    }

    public Map<String, Object> diaoDuRiZhiToMap(errorLogModel model, int num){
        Map<String, Object> data = new HashMap<>();
        data.put("QiYeMingCheng",model.getStr("QiYeMingCheng"));
        data.put("ChePaiHaoMa",model.getStr("ChePaiHaoMa"));
        data.put("ChePaiYanSe", model.getStr("ChePaiYanSe"));
        data.put("JiaShiYuan", model.getStr("JiaShiYuan"));
        data.put("ChuCheRiQi", DateFormatUtils.format(model.getDate("ChuCheRiQi"),"yyyy-MM-dd HH:mm"));
        data.put("ShiFaDian",model.getStr("ShiFaDian"));
        data.put("ZhongDaoDian",model.getStr("ZhongDaoDian"));
        data.put("ZhuangZaiWuMingCheng",model.getStr("ZhuangZaiWuMingCheng"));
        data.put("ZhuangZaiWuXingZhi",model.getStr("ZhuangZaiWuXingZhi"));
        data.put("ZhuangZaiWuShuLiang",model.getStr("ZhuangZaiWuShuLiang"));
        data.put("DiaoDuYuan",model.getStr("DiaoDuYuan"));
        data.put("num",num);
        return data;
    }

    public String endDay(int i){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar lastMonthEndDateCal = Calendar.getInstance();
        lastMonthEndDateCal.add(Calendar.MONTH,i);
        lastMonthEndDateCal.set(Calendar.DAY_OF_MONTH, lastMonthEndDateCal.getActualMaximum(Calendar.DAY_OF_MONTH));
        return format.format(lastMonthEndDateCal.getTime()) + " 23:59:59";
    }
}
